package com.huiren.ui.importUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.annotation.PostConstruct;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.huiren.service.PurOrderService;

import java.awt.BorderLayout;
import java.util.List;

@Component
public class XmlImportJPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(NetImportJPanel.class);
	private JTextField address;
	private JLabel lblNewLabel;
	private JButton selectButton;
	private JButton importButton;
	private JTextArea result;
	@Autowired
	private PurOrderService purOrderService;
	private JPanel panel;

	public XmlImportJPanel() {
		setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		add(panel, BorderLayout.NORTH);

		lblNewLabel = new JLabel("文件地址:");
		panel.add(lblNewLabel);

		address = new JTextField();
		panel.add(address);
		address.setColumns(20);

		selectButton = new JButton("选择文件");
		panel.add(selectButton);

		importButton = new JButton("导入");
		panel.add(importButton);
		result = new JTextArea();
		result.setEditable(false);
		this.add(new JScrollPane(result), BorderLayout.CENTER);

		importButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					List<String> results = purOrderService
							.insertFromFile(address.getText());
					for (String r : results) {
						result.setText(result.getText() + r);
					}
				} catch (Exception e1) {
					logger.error(e1);
				}
			}
		});

		selectButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// 选择文件 按钮
				JFileChooser c = new JFileChooser();
				c.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int rVal = c.showOpenDialog(XmlImportJPanel.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					String fileName = c.getSelectedFile().getName();
					address.setText(c.getCurrentDirectory().toString() + "\\"
							+ fileName);
				}
			}
		});
	}

	@PostConstruct
	public void initMethod() {
	}

}
